package com.itheima.web.controller.system.role;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Permission;
import com.itheima.domain.system.Role;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import com.itheima.web.controller.system.syslog.SysLogServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@WebServlet(value = "/system/role")
public class RoleServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if("list".equals(operation)){
            this.list(request,response);
        }else if("toAdd".equals(operation)){
            this.toAdd(request,response);
        }else if("save".equals(operation)){
            this.save(request, response);
        }else if("toEdit".equals(operation)){
            this.toEdit(request,response);
        }else if ("deleteMany".equals(operation)){
            this.deleteMany(request,response);
        }else if("edit".equals(operation)){
            this.edit(request,response);
        }
    }

    private void deleteMany(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<String> list = new ArrayList<>();
        String moduleIds = request.getParameter("moduleIds");

        String[] str = moduleIds.split(",");
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
            System.out.println(str[i]);
        }

        //System.out.println();
        roleService.deleteMany(list);
        response.sendRedirect(request.getContextPath() + "/system/role?operation=list");

        //记日志
        //SysLogServlet.logMethodForServlet(request);
    }

    private void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//        //进入列表页
//        //获取数据
        int page = 1;
        int size = 5;
        if(StringUtils.isNotBlank(request.getParameter("currentPage"))){
            page = Integer.parseInt(request.getParameter("currentPage"));
        }
        if(StringUtils.isNotBlank(request.getParameter("pageSize"))){
            size = Integer.parseInt(request.getParameter("pageSize"));
        }
        PageInfo all = roleService.findAll(page, size);
        //将数据保存到指定的位置
        request.setAttribute("page",all);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/role/role-list.jsp").forward(request,response);
    }

    private void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //加载所有的部门信息放入到roleList
        List<Role> all = roleService.findAll();
        request.setAttribute("roleList",all);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/role/role-add.jsp").forward(request,response);
    }

    private void save(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
        Role role = BeanUtil.fillBean(request, Role.class);

        //System.out.println(role);     id='null', permissions=null

        //调用业务层接口save
        roleService.save(role);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath()+"/system/role?operation=list");
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询要修改的数据toEdit
        String id = request.getParameter("id");
        Role role = roleService.findById(id);
        //将数据加载到指定区域，供页面获取
        request.setAttribute("role",role);

        //System.out.println(permissionList); 莫得问题


        //查找权限
        List<Permission> permissionList = roleService.findPermission(id);

        //System.out.println(permissionList); 莫得问题
        request.setAttribute("permissionList",permissionList);


        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/role/role-permission-add.jsp").forward(request,response);
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String roleId = request.getParameter("roleId");
        String[] ids = request.getParameterValues("ids");


        /*
        正确的获得了roleId和permission的id
        System.out.println(roleId);
        for (String id : ids) {
            System.out.println(id);
        }
        */


        //if (true) return;

        roleService.updateRolePermission(roleId,ids);


        response.sendRedirect(request.getContextPath()+"/system/role?operation=list");
    }

    /*private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //将数据获取到，封装成一个对象
        Role role = BeanUtil.fillBean(request, Role.class);
        //调用业务层接口save
        roleService.delete(role.getId());
        //跳转回到页面list
        response.sendRedirect(request.getContextPath()+"/system/role?operation=list");
    }*/


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}